<template>
  <view>
    <view class="category_tab">
      <view class="category_tab_title">
        <view class="title_inner">
          <uni-segmented-control
            :current="current"
            :values="items.map(v => v.title)"
            @clickItem="onClickItem"
            style-type="text"
            active-color="#d4237a"
          ></uni-segmented-control>
        </view>
        <view class="iconfont iconsearch"></view>
      </view>

      <scroll-view
        scroll-y
        enable-flex
        class="content"
        @scrolltolower="handleScrolltolower"
      >
        <view class="cate_item" v-for="(item,index) in vertical" :key="item.id">
          <go-detail :list="vertical" :index="index">
          <img :src="item.thumb" alt="" mode="widthFix" />
          </go-detail>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui";
import goDetail from "@/components/goDetail";
export default {
  data() {
    return {
      items: [
        { title: "最新", order: "new" },
        { title: "热门", order: "hot" }
      ],
      current: 0,
      params: {
        limit: 30,
        skip: 0,
        order: "new"
      },
      id: -1,
      vertical: [],
      hasMore: true
    };
  },
  components: {
    uniSegmentedControl,
    goDetail
  },
  onLoad(options) {
    this.id = options.id;
    this.getList();
  },
  methods: {
    onClickItem(index) {
      if (this.current !== index.currentIndex) {
        this.current = index.currentIndex;
      } else {
        return;
      }

      this.params.order = this.items[index.currentIndex].order;
      this.params.skip = 0;
      this.vertical = [];
      this.getList();
    },
    async getList() {
      const res = await this.request(
        `/image/v1/vertical/category/${this.id}/vertical`,
        this.params
      );
      if (res.code === 0) {
        if (res.res.vertical.length === 0) {
          this.hasMore = false;
          uni.showToast({
            title: "没有更多了",
            icon: "none"
          });
          return;
        }
        this.vertical = [...this.vertical, ...res.res.vertical];
      }
    },
    handleScrolltolower() {
      if (this.hasMore) {
        this.params.skip += this.params.limit;
        this.getList();
      } else {
        uni.showToast({
          title: "没有更多了",
          icon: "none"
        });
      }
    }
  }
};
</script>

<style lang="scss">
.category_tab {
  .category_tab_title {
    position: relative;
    .title_inner {
      width: 60%;
      margin: 0 auto;
    }
  }
}

.iconsearch {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 5%;
}
.content {
  height: calc(100vh - 36px);
  display: flex;
  flex-wrap: wrap;
  .cate_item {
    flex: 33.3333%;
    border: 1rpx solid #ccc;
    img {
    }
  }
}
</style>
